﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.settings
{
    public partial class sys_setting : He.Web.UI.Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowInfo();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            //语言模块
            List<Model.Lang> lst = new BLL.config().LoadConfig<List<Model.Lang>>(LangXmlPath);
            foreach (Model.Lang cl in lst)
            {
                ListItem li = new ListItem();
                li.Text = cl.Name;
                li.Value = cl.Value;
                li.Selected = cl.IsShow;
                cblLang.Items.Add(li);
            }
            //参数设置
            if (!string.IsNullOrEmpty(webset.qrurl))
            {
                qr_review.Text = string.Format("<img src=\"{0}\" width=\"110\" />", webset.qrurl);
            }

            //有道翻译
            txtKeyfrom.Text = webset.youdao_keyfrom;
            txtKey.Text = webset.youdao_key;
            //Bing翻译
            txtBingClientId.Text = webset.bing_client_id;
            txtBingClientSecret.Text = webset.bing_client_secret;
            //QQ登录
            txtQQAppId.Text = webset.qqappid;
            txtQQAppKey.Text = webset.qqappkey;

            rblUrlMode.DataSource = EnumHelper.GetList(typeof(Enums.UrlMode));
            rblUrlMode.DataTextField = "Text";
            rblUrlMode.DataValueField = "Value";
            rblUrlMode.DataBind();
            rblUrlMode.SelectedValue = webset.url_mode.ToString();

            rblWeixinStatus.SelectedValue = webset.wx_status.ToString();
            txtupload_file_type.Text = webset.upload_file_type;
            txtupload_file_size.Text = webset.upload_file_size.ToString();

            rblAdminlogsStatus.SelectedValue = webset.adminlogs_status.ToString();
            rblShowFeedback.SelectedValue = webset.show_feedback.ToString().ToLower();

            rblMemberRegStatus.DataSource = EnumHelper.GetList(typeof(Enums.MemberStatus));
            rblMemberRegStatus.DataTextField = "Text";
            rblMemberRegStatus.DataValueField = "Value";
            rblMemberRegStatus.DataBind();
            rblMemberRegStatus.SelectedValue = webset.member_reg_status.ToString();

            //语言模块下设置
            Model.site_info site_info = new BLL.site_info().GetModelByLang(lang);

            txtEnginePage.Text = site_info.EnginePage;
            if (!string.IsNullOrEmpty(site_info.LogoUrl))
            {
                logo_review.Text = string.Format("<img src=\"{0}\" height=\"50\" />", site_info.LogoUrl);
            }

            List<Model.plug> pluglst = new BLL.config().LoadConfig<List<Model.plug>>(PlugXmlPath);
            int i = 0;
            foreach (Model.plug cl in pluglst)
            {
                ListItem li = new ListItem();
                li.Text = cl.Name;
                li.Value = cl.Value;
                li.Selected = cl.IsShow;
                cblPlug.Items.Add(li);
                if (cl.Lang != lang)
                {
                    cblPlug.Items[i].Attributes["style"] = "display:none";
                }
                i++;
            }
        }
        #endregion

        //保存配置
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //保存语言模块
            BLL.config mgr = new He.BLL.config();
            List<Model.Lang> lst = new List<Model.Lang>();
            foreach (ListItem li in cblLang.Items)
            {
                Model.Lang cl = new Model.Lang();
                cl.Name = li.Text;
                cl.Value = li.Value;
                cl.IsShow = li.Selected;
                lst.Add(cl);
            }
            mgr.SaveConfig<List<Model.Lang>>(lst, LangXmlPath);
            //保存参数设置
            BLL.webset bll = new BLL.webset();
            Model.webset model = bll.GetWebset();
            //有道翻译
            model.youdao_keyfrom = txtKeyfrom.Text.Trim();
            model.youdao_key = txtKey.Text.Trim();
            //Bing翻译
            model.bing_client_id = txtBingClientId.Text.Trim();
            model.bing_client_secret = txtBingClientSecret.Text.Trim();
            //QQ登录
            model.qqappid = txtQQAppId.Text.Trim();
            model.qqappkey = txtQQAppKey.Text.Trim();

            HttpPostedFile _posteQr = qrurl.PostedFile;
            if (_posteQr != null && _posteQr.FileName != string.Empty)
            {
                string _fileExt = _posteQr.FileName.Substring(_posteQr.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(UploadImgExt, _fileExt))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>不允许上传" + _fileExt + "类型的文件,请重试。", "back", "Error");
                    return;
                }
                if (_posteQr.ContentLength > webset.upload_file_size * 1024)
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>图片超过限制，请先压缩图片。", "back", "Error");
                    return;
                }
                FileHelper.DeleteFile(webset.qrurl);
                string _qrurl = "/images/sys/qr." + _fileExt;
                _posteQr.SaveAs(Server.MapPath(_qrurl));
                model.qrurl = _qrurl;
            }
            model.url_mode = int.Parse(rblUrlMode.SelectedValue);
            model.wx_status = int.Parse(rblWeixinStatus.SelectedValue);
            model.upload_file_type = txtupload_file_type.Text.Trim();
            model.upload_file_size = int.Parse(txtupload_file_size.Text.Trim());
            model.adminlogs_status = int.Parse(rblAdminlogsStatus.SelectedValue);
            model.show_feedback = bool.Parse(rblShowFeedback.SelectedValue);
            model.member_reg_status = int.Parse(rblMemberRegStatus.SelectedValue);

            bll.SaveConfig(model);
            //保存语言模块下设置
            siteinfo.EnginePage = txtEnginePage.Text.Trim();

            HttpPostedFile _posteLogo = logourl.PostedFile;
            if (_posteLogo != null && _posteLogo.FileName != string.Empty)
            {
                string _fileExt = _posteLogo.FileName.Substring(_posteLogo.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(UploadImgExt, _fileExt))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>不允许上传" + _fileExt + "类型的文件,请重试。", "back", "Error");
                    return;
                }
                if (_posteLogo.ContentLength > model.upload_file_size * 1024)
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>图片超过限制，请先压缩图片。", "back", "Error");
                    return;
                }
                FileHelper.DeleteFile(siteinfo.LogoUrl);
                string _logourl = "/templates/" + siteinfo.Template + "/images/logo" + "." + _fileExt;
                _posteLogo.SaveAs(Server.MapPath(_logourl));
                siteinfo.LogoUrl = _logourl;
            }
            new BLL.config().SaveConfig<Model.site_info>(siteinfo, string.Format("~/config/lang_{0}.config", this.lang));

            List<Model.plug> pluglst = new List<Model.plug>();
            List<Model.plug> pluglst2 = new He.BLL.config().LoadConfig<List<Model.plug>>(PlugXmlPath);
            int i = 0;
            foreach (Model.plug cl in pluglst2)
            {
                cblPlug.Items[i].Attributes["ManageUrl"] = cl.ManageUrl;
                cblPlug.Items[i].Attributes["Lang"] = cl.Lang;
                i++;
            }
            foreach (ListItem li in cblPlug.Items)
            {
                Model.plug cl = new Model.plug();
                cl.Name = li.Text;
                cl.Value = li.Value;
                cl.ManageUrl = li.Attributes["ManageUrl"];
                cl.Lang = li.Attributes["Lang"];
                cl.IsShow = li.Selected;
                pluglst.Add(cl);

            }
            mgr.SaveConfig<List<Model.plug>>(pluglst, PlugXmlPath);
            RemoveAllCache();
            //SaveLogs("[控制面板]修改系统设置");
            ShowSuccess("系统设置修改成功啦！", Request.RawUrl);
        }

        //删除二维码操作
        protected void lbtnDelQr_Click(object sender, EventArgs e)
        {
            FileHelper.DeleteFile(webset.qrurl);
            webset.qrurl = "";
            new BLL.webset().SaveConfig(webset);
            Response.Redirect(Request.RawUrl);
        }

        //删除手机网站logo操作
        protected void lbtnDelLogo_Click(object sender, EventArgs e)
        {
            FileHelper.DeleteFile(siteinfo.LogoUrl);
            siteinfo.LogoUrl = "";
            new BLL.config().SaveConfig<Model.site_info>(siteinfo, string.Format("~/config/lang_{0}.config", this.lang));
            Response.Redirect(Request.RawUrl);
        }
    }
}